#include "zygtimer.h"

ZygTimer::ZygTimer()
{
    this->alarmClock = nullptr;
    this->soundFilePath = nullptr;
    this->soundFileName = nullptr;
}

ZygTimer::~ZygTimer()
{
    if(this->alarmClock != nullptr){
        delete this->alarmClock;
    }

}

void ZygTimer::setHour(int hour)
{
    this->hour = hour;
}

int ZygTimer::getHour()
{
    return this->hour;
}

void ZygTimer::setMinute(int minute)
{
    this->minute = minute;
}

int ZygTimer::getMinute()
{
    return this->minute;
}

void ZygTimer::setSecond(int second)
{
    this->second = second;
}

int ZygTimer::getSecond()
{
    return this->second;
}

void ZygTimer::setSoundFilePath(QString soundFilePath)
{
    this->soundFilePath = soundFilePath;
}

QString ZygTimer::getSoundFilePath()
{
    return this->soundFilePath;
}

QString ZygTimer::toTimeString()
{
    return this->getHourString() + ":" + this->getMinuteString() + ":" +this->getSecondString();
}


QString ZygTimer::getHourString()
{
    return QString("%1").arg(this->hour,2,10,QLatin1Char('0'));
}

QString ZygTimer::getMinuteString()
{
    return QString("%1").arg(this->minute,2,10,QLatin1Char('0'));
}

QString ZygTimer::getSecondString()
{
    return QString("%1").arg(this->second,2,10,QLatin1Char('0'));
}


QString ZygTimer::toString()
{
    QString res = "将会在";
    res += this->getHourString();
    res += ":";
    res += this->getMinuteString();
    res += ":";
    res += this->getSecondString();
    res += "播放";
    res += "\r\n";
    res += this->getFileName();
    return res;
}

QString ZygTimer::toFormatString(){
    QString res = this->getHourString();
    res += ":";
    res += this->getMinuteString();
    res += ":";
    res += this->getSecondString();
    res += "\t";
    res += this->getFileName();
    res += "\r\n";
    return res;
}

void ZygTimer::print()
{
    qDebug() << this->toString();
}

void ZygTimer::createAlarmClock(){

    if(this->alarmClock != nullptr){
        delete this->alarmClock;
    }

    QTime time = QTime::currentTime();
    //等待小时
    int waitHour = this->hour - time.hour();
    //等待分钟
    int waitMinute = this->minute - time.minute();
    //等待秒钟
    int waitSecond = this->second - time.second();

    ZygTimer *temp = new ZygTimer();
    temp->setHour(time.hour());
    temp->setMinute(time.minute());
    temp->setSecond(time.second());

    if( (*this) < temp){
        waitHour += 23;
        waitMinute += 59;
        waitSecond += 60;
    }else if ((*this) < temp){
        waitHour = 24;
        waitMinute = 0;
        waitSecond = 0;
    }

    // 设定时间，给一定的余量
    int wait = waitHour * HOUR_TO_SECOND + waitMinute * MINUTE_TO_SECOND + waitSecond - 3;
//    qDebug() << "当前时间" << time.hour() << ":" << time.minute() <<"：" << time.second();
//    qDebug() << "等待" << waitHour << "小时" << waitMinute << "分钟" << waitSecond << "秒" << " 一共" << wait << "秒";
    this->alarmClock = new QTimer();
    connect(this->alarmClock,&QTimer::timeout,[=](){
//        qDebug() << "定时器执行时间" << this->toString();
        ZygPlayer::play();
        alarmClock->setInterval(24 * 3600 * 1000);
    });
    alarmClock->setInterval(wait * 1000);
    alarmClock->start();
}

ZygClicLinkedList<ZygTimer>* ZygTimer::timerList = new ZygClicLinkedList<ZygTimer>;
void ZygTimer::printList()
{
    int size = ZygTimer::timerList->length();
    qDebug() << "设定了" << size << "个闹钟";
    ZygTimer::timerList->forEachPrint();
}

bool ZygTimer::addTimer(ZygTimer *t)
{
    return ZygTimer::timerList->addSort(t);
}

void ZygTimer::deleteTimer(int i)
{
    ZygTimer::timerList->remove(i);
}


bool ZygTimer::operator<(ZygTimer* other){
    if(this->hour < other->hour){
        return true;
    }else if(this->hour > other->hour) {
        return false;
    }else {
        if(this->minute < other->minute){
            return true;
        }else if(this->minute > other->minute) {
            return false;
        }else {
            return this->second < other->second;
        }
    }
}

bool ZygTimer::operator>(ZygTimer* other){
    if(this->hour < other->hour){
        return false;
    }else if(this->hour > other->hour) {
        return true;
    }else {
        if(this->minute < other->minute){
            return false;
        }else if(this->minute > other->minute) {
            return true;
        }else {
            return this->second > other->second;
        }
    }
}

QString ZygTimer::getFileName(){
    if(1){
        return "章鱼哥下班";
    }
    if(stringIsEmpty(this->soundFileName)
            || stringIsEmpty(this->soundFilePath)){
        this->soundFilePath = DEFAULT_SOUND_FILE_PATH;
    }
    if(this->soundFileName != nullptr){
        return this->soundFileName;
    }

    int lastSlash = this->soundFilePath.lastIndexOf("\\");
    if(lastSlash == -1){
        lastSlash = this->soundFilePath.lastIndexOf("/");
    }
    int dot = this->soundFilePath.indexOf('.');
    return this->soundFileName = this->soundFilePath.mid(lastSlash + 1,dot - lastSlash - 1);
}


bool ZygTimer::operator== (ZygTimer* other){
    return this->hour == other->hour && this->minute == other->minute && this->second==other->second;
}

int ZygTimer::listSize(){
    return ZygTimer::timerList->length();
}

ZygTimer* ZygTimer::get(int index){
    return ZygTimer::timerList->get(index);
}

void ZygTimer::clearList(){
    ZygTimer::timerList->clear();
//    qDebug() << ZygTimer::listSize();
}

QString ZygTimer::listToFormatString(){
    return ZygTimer::timerList->toFormatString();
}





